package org.jeecg.modules.performance.job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.JsonObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.publish.entity.ThirdContentLink;
import org.jeecg.modules.publish.entity.ThirdReadNum;
import org.jeecg.modules.publish.service.IThirdContentLinkService;
import org.jeecg.modules.publish.service.IThirdReadNumService;
import org.jeecg.modules.publish.util.AccessTokenUtil;
import org.jeecg.modules.third.entity.ThirdAccount;
import org.jeecg.modules.third.entity.ThirdInfo;
import org.jeecg.modules.third.service.IThirdAccountService;
import org.jeecg.modules.third.service.IThirdInfoService;
import org.jeecg.util.DateTimeUtil;
import org.jeecg.util.DouYinUtil;
import org.jeecgframework.p3.core.util.HttpUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class PerformanceDouYinReadJob implements Job {
    //
    @Autowired
    private IThirdContentLinkService thirdContentLinkService;
    @Autowired
    private IThirdReadNumService thirdReadNumService;
    @Autowired
    private IThirdAccountService thirdAccountService;
    @Autowired
    private IThirdInfoService thirdInfoService;
    @SneakyThrows
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String startTime = DateTimeUtil.getLastDayCharN8En(30 );
        String endTime = DateTimeUtil.getLastDayCharN8En(1);
        QueryWrapper<ThirdContentLink> queryWrapper = new QueryWrapper<>();
        queryWrapper.ge("data",startTime);
        queryWrapper.le("data",endTime);
        queryWrapper.eq("type",3);
        List<ThirdContentLink> list = thirdContentLinkService.list(queryWrapper);


        if(!CollectionUtils.isEmpty(list)){


            //
            //Map<String, String> collect = list.stream().collect(Collectors.toMap(ThirdContentLink::getId, ThirdContentLink::getContentId));
            Map<String, List<ThirdContentLink>> accountcollect = list.stream().collect(Collectors.groupingBy(x->x.getAccountId()));

            for (String s : accountcollect.keySet()) {
                List<String> thirdIs = new ArrayList<>();
                List<ThirdContentLink> thirdContentLinks = accountcollect.get(s);
                for (ThirdContentLink thirdContentLink : thirdContentLinks) {
                    thirdIs.add(thirdContentLink.getId());
                }

                ThirdAccount thirdAccount = thirdAccountService.getById(s);
                ThirdInfo thirdInfo = thirdInfoService.getById(thirdAccount.getBaseId());
                Map<String, String> douYinInfo = DouYinUtil.getDouYinInfo(thirdInfo.getClientKey(),thirdInfo.getClientSecret(),thirdAccount.getAuthorizationCode());

             String token =  douYinInfo.get("access_token");
            String openId = douYinInfo.get("open_id");
//
//
//            for (ThirdContentLink thirdContentLink : list) {
//                thirdIs.add(thirdContentLink.getThirdContentId());
//
//            }
            String url = "https://open.douyin.com/video/data?open_id="+openId +"&access_token="+token;
            String result = HttpUtils.doPostJson(url, JSONObject.toJSONString(thirdIs));

            JSONArray data = JSON.parseObject(result).getJSONArray("data");
            List<ThirdContentLink> thirdList = new ArrayList<>();

            for (Object datum : data) {
                JSONObject jsonObject = JSON.parseObject(datum.toString());
                String objectId  = jsonObject.getString("item_id");
                //阅读数
                Integer  count = jsonObject.getJSONObject("statistics").getInteger("play_count");
                //转发数
                Integer  forwardCount = jsonObject.getJSONObject("statistics").getInteger("forward_count");
                //点赞数
                Integer  like = jsonObject.getJSONObject("statistics").getInteger("digg_count");


                ThirdContentLink thirdContentLink = new ThirdContentLink();

                thirdContentLink.setLoveNum(like);

                thirdContentLink.setId(objectId);
                thirdContentLink.setReadNum(count);
                thirdContentLink.setReportNum(forwardCount);
                thirdList.add(thirdContentLink);
            }


            if(!CollectionUtils.isEmpty(thirdList)){
                thirdContentLinkService.saveOrUpdateBatch(thirdList);
            }
        }
        }

    }
}
